package com.share.lottery.http;

import java.io.IOException;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.util.Log;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;
import com.share.lottery.base.Constants;
import com.share.lottery.model.AdvertBean;
import com.share.lottery.model.BaseEntity;
import com.share.lottery.model.User;
import com.share.lottery.utils.JsonUtil;
import com.share.lottery.utils.StringUtil;
/**
 * 用户相关 
 * @author py
 *
 */
public class UsesrController extends BaseController {

	private static UsesrController singleton;
	private List<AdvertBean> AdvertBeanList;

	private UsesrController() {
	}

	public static UsesrController getInstance() {
		if (singleton == null) {
			singleton = new UsesrController();
		}
		return singleton;
	}


	/**
	 * 获取注册短信验证码
	 * @param handler
	 * @param phone
	 */
	public synchronized void requestVerifacation(final Handler handler,final String phone) {

		final int flag = Constants.VERIFACATION;

		String url = Constants.caibo_rmd_comment_get;

		RequestParams params = new RequestParams();
		params.addBodyParameter("mobile", phone);
		
		LogUtils.e("url===" + url + "?" + params.getEntity());

		httpUtils.configCurrentHttpCacheExpiry(1000 * 1);
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {

					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub

						LogUtils.e("responseInfo===" + responseInfo.toString());
						BaseEntity entity = null;
						try {
							entity = JsonUtil.getObject(responseInfo.result
									+ "", BaseEntity.class);

						} catch (com.alibaba.fastjson.JSONException e) {
							e.printStackTrace();
							sendMsg(handler, flag, Constants.HTTP_ERROR);
							return;
						}

						LogUtils.e("entity===" + entity.toString());

						if ("1".equals(entity.getStatusCode())) {// 请求成功，数据返回正常

							if (!StringUtil.isNullOrEmpty(entity.getData())) {
								sendMsg(handler, flag, Constants.HTTP_OK, null);
							} else {
								sendMsg(handler, flag, Constants.HTTP_OK, null);
							}

						} else if ("0".equals(entity.getStatusCode())) {// 请求出现错误
							sendMsg(handler, flag, Constants.HTTP_ERROR,
									entity.getData());
						} else if ("-1".equals(entity.getStatusCode())) {// 登录超时，需要重新登录
							// sendMsg(handler, flag, Constants.HTTP_TIMEOUT);
//							ReLoginUtils.reLogin(new ReLoginCallBack() {
//
//								@Override
//								public void login_success() {
//									// TODO Auto-generated method stub
//									requestAdvertBeanList(handler);
//								}
//
//								@Override
//								public void login_Fail() {
//									// TODO Auto-generated method stub
//									BaseApplication
//											.showToast("超时重登录失败，请退出重新登录！");
//								}
//							});
						} else {
							sendMsg(handler, flag, Constants.HTTP_ERROR,
									entity.getData());
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// TODO Auto-generated method stub
						LogUtils.e("error===" + error.toString());
						sendMsg(handler, flag, Constants.HTTP_ERROR_NET);
					}
				});

	}

	/**
	 * 获取通讯录列表
	 * 
	 * @return
	 */
	public List<AdvertBean> getAdvertBeanList() {
		return AdvertBeanList;
	}

	public void setAdvertBeanList(List<AdvertBean> AdvertBeanList) {
		if (AdvertBeanList == null) {
			return;
		}
		if (this.AdvertBeanList == null) {
			this.AdvertBeanList = AdvertBeanList;
		} else {
			this.AdvertBeanList.addAll(AdvertBeanList);
		}
	}

	/**
	 * 清除通讯录列表
	 */
	public void clearAdvertBeanList() {
		// TODO Auto-generated method stub
		this.AdvertBeanList = null;
	}

		
	public synchronized void login(final Handler handler,final String userName,final String userPass)   {
		final int flag = Constants.login;
		String url = Constants.BASE_IP+"api/login_valid";
		RequestParams params = new RequestParams();
		params.addBodyParameter("username", userName);
		
		params.addBodyParameter("password", userPass);

		try {
			LogUtils.e("url===" + url + "?" + params.getEntity().getContentType());
		} catch (IllegalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Log.i("test", "请求的接口和数据=="+ url);
		
		httpUtils.send(HttpMethod.POST, url,params, 
				new RequestCallBack<String>() {

			public void onStart() {

			}


			public void onSuccess(ResponseInfo<String> responseInfo) {
			

				LogUtils.e("responseInfo===" + responseInfo.result);
				BaseEntity entity = null;
				try {
					entity = JsonUtil.getObject(responseInfo.result
							+ "", BaseEntity.class);

				} catch (com.alibaba.fastjson.JSONException e) {
					e.printStackTrace();
					sendMsg(handler, flag, Constants.HTTP_ERROR);
					return;
				}

				LogUtils.e("entity===" + entity.toString());
	
				if ("1".equals(entity.getStatusCode())) {// 请求成功，数据返回正常

					if (!StringUtil.isNullOrEmpty(entity.getData())) {
						sendMsg(handler, flag, Constants.HTTP_OK, entity.getData());
						
					} else {
						sendMsg(handler, flag, Constants.HTTP_OK, null);
					}

				} else if ("0".equals(entity.getStatusCode())) {// 请求出现错误
					sendMsg(handler, flag, Constants.HTTP_ERROR,
							entity.getData());
				} else if ("-1".equals(entity.getStatusCode())) {// 登录超时，需要重新登录

				} else {
					sendMsg(handler, flag, Constants.HTTP_ERROR,
							entity.getData());
				}
			}
			public void onFailure(HttpException error, String msg) {
				LogUtils.e("error===" + error.toString());
				sendMsg(handler, flag, Constants.HTTP_ERROR_NET);
			}
		});

	}
	
	
	public synchronized void getUserInfo(final Handler handler,final String token)   {
		final int flag = Constants.loginToken;
		String url = Constants.BASE_IP+"api/get/user";
		RequestParams params = new RequestParams();
		params.addBodyParameter("token", token);
	

		try {
			LogUtils.e("url===" + url + "?" + params.getEntity().getContentType());
		} catch (IllegalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Log.i("test", "请求的接口和数据=="+ url);
		
		httpUtils.send(HttpMethod.POST, url,params, 
				new RequestCallBack<String>() {

			public void onStart() {

			}


			public void onSuccess(ResponseInfo<String> responseInfo) {
			

				LogUtils.e("responseInfo===" + responseInfo.result);
				BaseEntity entity = null;
				try {
				
					entity = JsonUtil.getObject(responseInfo.result
							+ "", BaseEntity.class);

				} catch (com.alibaba.fastjson.JSONException e) {
					e.printStackTrace();
					sendMsg(handler, flag, Constants.HTTP_ERROR);
					return;
				}

				LogUtils.e("entity===" + entity.toString());
	
				if ("1".equals(entity.getStatusCode())) {// 请求成功，数据返回正常

					if (!StringUtil.isNullOrEmpty(entity.getData())) {
						try {
						
							User user = JsonUtil.getObject(	new JSONArray(entity.getData().toString()).get(0).toString(), User.class);
							sendMsg(handler, flag, Constants.HTTP_OK, user);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
					} else {
						sendMsg(handler, flag, Constants.HTTP_OK, null);
					}

				} else if ("0".equals(entity.getStatusCode())) {// 请求出现错误
					sendMsg(handler, flag, Constants.HTTP_ERROR,
							entity.getData());
				} else if ("-1".equals(entity.getStatusCode())) {// 登录超时，需要重新登录

				} else {
					sendMsg(handler, flag, Constants.HTTP_ERROR,
							entity.getData());
				}
			}
			public void onFailure(HttpException error, String msg) {
				LogUtils.e("error===" + error.toString());
				sendMsg(handler, flag, Constants.HTTP_ERROR_NET);
			}
		});

	}
	/**
	 * @param handler
	 * @param username 手机号码
	 * @param avatar  头像
	 * @param nickname  昵称
	 * @param gender 性别
	 * @param babyBirthAt 宝贝出生日期
	 * @param cityId 城市id
	 */
	public synchronized void register(final Handler handler,final String username,final String avatar,
			final String nickname,final String gender,final String babyBirthAt,final String cityId,final String code)   {
		final int flag = Constants.login;
		String url = Constants.BASE_IP+"api/reg";
		RequestParams params = new RequestParams();
		params.addBodyParameter("username", username);
		params.addBodyParameter("avatar", avatar);
		params.addBodyParameter("nickname", nickname);
		params.addBodyParameter("gender", gender);
		params.addBodyParameter("babyBirthAt", babyBirthAt);
		params.addBodyParameter("cityId", cityId);
		params.addBodyParameter("code", code);

		try {
			LogUtils.e("url===" + url + "?" + params.getEntity().getContentType());
		} catch (IllegalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Log.i("test", "请求的接口和数据=="+ url);
		
		httpUtils.send(HttpMethod.POST, url,params, 
				new RequestCallBack<String>() {

			public void onStart() {

			}


			public void onSuccess(ResponseInfo<String> responseInfo) {
			

				LogUtils.e("responseInfo===" + responseInfo.result);
				BaseEntity entity = null;
				try {
					entity = JsonUtil.getObject(responseInfo.result
							+ "", BaseEntity.class);

				} catch (com.alibaba.fastjson.JSONException e) {
					e.printStackTrace();
					sendMsg(handler, flag, Constants.HTTP_ERROR);
					return;
				}

				LogUtils.e("entity===" + entity.toString());
	
				if ("1".equals(entity.getStatusCode())) {// 请求成功，数据返回正常

					if (!StringUtil.isNullOrEmpty(entity.getData())) {
						sendMsg(handler, flag, Constants.HTTP_OK, entity.getData());
						
					} else {
						sendMsg(handler, flag, Constants.HTTP_OK, null);
					}

				} else if ("0".equals(entity.getStatusCode())) {// 请求出现错误
					sendMsg(handler, flag, Constants.HTTP_ERROR,
							entity.getData());
				} else if ("-1".equals(entity.getStatusCode())) {// 登录超时，需要重新登录

				} else {
					sendMsg(handler, flag, Constants.HTTP_ERROR,
							entity.getData());
				}
			}
			public void onFailure(HttpException error, String msg) {
				LogUtils.e("error===" + error.toString());
				sendMsg(handler, flag, Constants.HTTP_ERROR_NET);
			}
		});

	}
	
	
}
